import React from 'react';
import { hydrateRoot } from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import App from '../shared/App';
import { InitialStateDataType } from '../shared/context/InitialStateContext';

const rootElement = document.getElementById('root');
if (!rootElement) throw new Error('Failed to find the root element');

declare global {
  interface Window {
    __INITIAL_STATE__?: InitialStateDataType;
  }
}
const getInitialState = () => {
  if (typeof window === 'undefined') return {};
  if (!window.__INITIAL_STATE__) return {};
  try {
    return typeof window.__INITIAL_STATE__ === 'string' ? JSON.parse(window.__INITIAL_STATE__) : window.__INITIAL_STATE__;
  } catch (error) {
    return {};
  }
};

hydrateRoot(
  rootElement,
  <BrowserRouter basename="/website">
    <App initialState={getInitialState()} />
  </BrowserRouter>
); 